#include <iostream>
#include <string.h>
using namespace std;

class String {
public:
    String(const char *str = NULL);
    String(const String &other);
    ~String(void);
    String & operator=(const String &other);
private:
    char *m_data;
};

String::String(const char *str)
{
    cout << "default constructor from " << str << endl;
    if (str == NULL) {
        m_data = new char[1];
        *m_data = '\0';
    }
    else {
        int length = strlen(str);
        m_data = new char[length+1];
        strcpy(m_data, str);
    }
}

String::String(const String &other)
{
    cout << "copy constructor from " << other.m_data << endl;
    int length = strlen(other.m_data);
    m_data = new char[length+1];
    strcpy(m_data, other.m_data);
}

String::~String(void)
{
    cout << "deconstructor..." <<  m_data << endl;
    delete [] m_data;
}

String & String::operator=(const String &other)
{
    cout << "operateor = from " << other.m_data << endl;
    if (this == &other)
        return *this;

    delete [] m_data;

    int length = strlen(other.m_data);
    m_data = new char[length+1];
    strcpy(m_data, other.m_data);

    return *this;
}

void test()
{
    String str1("hello ");
    String str2 = "world ";
    String str3(str1);
    String str4 = str1;
    str3 = "c++";
    str4 = str1;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}
